home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 14 / Mac Magazin and MacEasy Magazine CD - Issue 14.iso / Wissenschaft & Technik / Tools Plus 2.6.1 Evaluation Kit / Tools Plus 2.6.1 / Tutorials / 3-List Boxes / Tutorial.c < prev    next >
C/C++ Source or Header  |  1995-08-01  |  7KB  |  221 lines

  1. //    Tools Plus Tutorial -- List Boxes
  2.  
  3.  
  4.  
  5.  
  6.  #include "ToolsPlus.h"
  7.  
  8.  
  9.  
  10.  #define LeftList            1                // Constants to make code more readable…
  11.  #define RightList        2
  12.  
  13.  #define MoveButton        1
  14.  #define RemoveButton    2
  15.  #define DoneButton        255
  16.  
  17.  #define LeftToRight    1
  18.  #define RightToLeft    -1
  19.  
  20.  
  21.  
  22.  TPPollRecord        Poll;                    // Polling record to retrieve event information
  23.  Boolean                ExitTheDemo;    // Should the demo terminate?
  24.  short          Direction;        // Direction in which lines are moved (left-to-right
  25.                                                              //    or right-to-left)
  26.  
  27.  
  28.  
  29.  void ApplicationInitialization (void);
  30.  void ActionInWindow (void);
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  //    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -
  38.  void main(void)
  39.  {
  40.     InitGraf(&qd.thePort);
  41.     InitFonts();
  42.     InitWindows();
  43.     InitMenus();
  44.     TEInit();
  45.     InitDialogs(0L);
  46.     MaxApplZone();
  47.  
  48.  
  49.     if (!InitToolsPlus(1, 1, UseColor))
  50.         ExitToShell();
  51.  
  52.     ApplicationInitialization();
  53.  
  54.     while (!ExitTheDemo)                    //Main Event Loop
  55.         if (PollSystem(&Poll))            //If an event is available, process the event…
  56.             if ((Poll.What == doListBox) || (Poll.What == doButton))    // User clicked something in this window
  57.                 ActionInWindow();
  58.  
  59.     // All other events are ignored
  60.  }
  61.  
  62.  
  63.  
  64.  //    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -
  65.  void ApplicationInitialization (void)
  66.     {
  67.     //    Do all the application setup before you start polling for events…
  68.  
  69.      #define        DemoWindow1        1
  70.  
  71.     FontInfo    Font;                    // Info about current font
  72.     short            BoxHeight;        // List box's height in pixels
  73.  
  74.     short            resCount;            // Number of resources
  75.     short            resIndex;            // Resource index counter
  76.     Handle        hRsrc;                // Handle to a resource
  77.     short            theResID;            // Resource's ID
  78.     ResType        theResType;        // Resource's Type
  79.     Str255        theResName;        // Resource's Name
  80.     short            InsertLineAt;    // Location where line should be inserted in list to be
  81.                                                     //    in alphabetic order
  82.  
  83.  
  84.     WindowOpen(DemoWindow1, 0, 0, 440, 130, "\p", dBoxProc + wCenter, NoGoAway, Modal);
  85.  
  86.     // Create left list box, making it exactly 6 lines high using Chicago 12pt…
  87.     TextFont(systemFont);
  88.     TextSize(12);
  89.     GetFontInfo(&Font);
  90.     BoxHeight = 6 * (Font.ascent + Font.descent + Font.leading);
  91.     NewListBox(LeftList, 15, 15, 145, 15 + BoxHeight, lOnlyOne);
  92.  
  93.     // Create left list box, making it exactly 8 lines high using Geneva 9pt…
  94.     TextFont(geneva);
  95.     TextSize(9);
  96.     GetFontInfo(&Font);
  97.     BoxHeight = 8 * (Font.ascent + Font.descent + Font.leading);
  98.     NewListBox(RightList, 280, 15, 410, 15 + BoxHeight, lOnlyOne);
  99.  
  100.     // Create the buttons used to control these lists…
  101.     NewButton(MoveButton, 175, 20, 265, 40, "\pMove", pushButProc, disabled, notSelected);
  102.     NewButton(RemoveButton, 175, 50, 265, 70, "\pRemove", pushButProc, disabled, notSelected);
  103.     NewButton(DoneButton, 185, 92, 255, 112, "\pDone", pushButProc, enabled, notSelected);
  104.  
  105.  
  106.  
  107.     // Populate the left list with the names of all the fonts.    We are assuming that
  108.     // you are using System 6 or later, which uses 'FOND' resourses for fonts.
  109.     DrawListBox(LeftList, false);    // Stop list box drawing for faster speed
  110.     SetResLoad(false);                        // Don't load resources for more speed
  111.  
  112.     resCount = CountResources('FOND');    // How many fonts are there?
  113.     for (resIndex = 1; resIndex <= resCount; resIndex++)
  114.         {
  115.         hRsrc = GetIndResource('FOND', resIndex);                            // Get a handle to the font
  116.         GetResInfo(hRsrc, &theResID, &theResType, theResName);// Get the font's ID, Type and Name
  117.  
  118.         InsertLineAt = SearchListBox(LeftList, theResName);        // Where should this name go?
  119.         InsertListBoxLine(LeftList, InsertLineAt);                        // Insert an empty line
  120.         SetListBoxText(LeftList, InsertLineAt, theResName);        // Put font name into empty line
  121.         }
  122.     SetResLoad(true);                            // Resume loading resources
  123.     DrawListBox(LeftList, true);    // Draw the list box's contents
  124.  
  125.     Direction = none;
  126.     ExitTheDemo = false;
  127.     }
  128.     
  129.  
  130.  
  131.  //    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -
  132.  void ActionInWindow (void)
  133.     {
  134.     short            SourceList, TargetList;    // Source and target list when moving lines
  135.     short            SourceLine, TargetLine;    // Line number in source and target list
  136.     Str255        FontName;
  137.  
  138.  
  139.     CurrentWindow(1);        // Subsequent actions affect window #1 (our only window)
  140.     switch (Poll.What)
  141.         {
  142.         case doListBox: 
  143.             if (GetListBoxLines(Poll.ListBox.Num, 1) == none)    // If an empty line was clicked…
  144.                 {        // Nothing selected, so disable the buttons and deselect lines in both lists…
  145.                 Direction = none;
  146.                 ButtonTitle(MoveButton, "\pMove");
  147.                 EnableButton(MoveButton, off);
  148.                 EnableButton(RemoveButton, off);
  149.                 SetListBoxLine(LeftList, max(1, GetListBoxLines(LeftList, 1)), off);
  150.                 SetListBoxLine(RightList, max(1, GetListBoxLines(RightList, 1)), off);
  151.                 }
  152.             else
  153.                 {        // If a non-empty line was clicked in the list box…
  154.                 switch (Poll.ListBox.Num)
  155.                     {
  156.                     case LeftList: 
  157.                         Direction = LeftToRight;
  158.                         ButtonTitle(MoveButton, "\p>> Move >>");
  159.                         // Turn off the line in the other list (if it has one selected)…
  160.                         SetListBoxLine(RightList, max(1, GetListBoxLines(RightList, 1)), off);
  161.                         break;
  162.  
  163.                     case RightList: 
  164.                         Direction = RightToLeft;
  165.                         ButtonTitle(MoveButton, "\p<< Move <<");
  166.                         // Turn off the line in the other list (if it has one selected)…
  167.                         SetListBoxLine(LeftList, max(1, GetListBoxLines(LeftList, 1)), off);
  168.                         break;
  169.                     }
  170.                     EnableButton(MoveButton, on);
  171.                     EnableButton(RemoveButton, on);
  172.                 }
  173.             break;
  174.  
  175.         case doButton: 
  176.             switch (Poll.Button.Num)
  177.                 {
  178.                 case MoveButton:        // Move line from source list to target list…
  179.                     if (Direction == LeftToRight)
  180.                         {
  181.                         SourceList = LeftList;
  182.                         TargetList = RightList;
  183.                         ButtonTitle(MoveButton, "\p<< Move <<");
  184.                         }
  185.                     else
  186.                         {
  187.                         SourceList = RightList;
  188.                         TargetList = LeftList;
  189.                         ButtonTitle(MoveButton, "\p>> Move >>");
  190.                         }
  191.                     SourceLine = GetListBoxLines(SourceList, 1);            // Find selected line in source list
  192.                     GetListBoxText(SourceList, SourceLine, FontName);    // Get selected line's text
  193.                     DeleteListBoxLine(SourceList, SourceLine);                // Delete line in source list
  194.  
  195.                     TargetLine = SearchListBox(TargetList, FontName);    // Where should this name go in target list?
  196.                     InsertListBoxLine(TargetList, TargetLine);                // Insert an empty line
  197.                     SetListBoxText(TargetList, TargetLine, FontName);    // Put font name into empty line
  198.                     SetListBoxLine(TargetList, TargetLine, on);                // Select line in target list
  199.                     Direction = -Direction;                                                        // Change direction of movement
  200.                     break;
  201.  
  202.                 case RemoveButton:    // Remove the selected line…
  203.                     if (Direction == LeftToRight)
  204.                         SourceList = LeftList;
  205.                     else
  206.                         SourceList = RightList;
  207.                     DeleteListBoxLine(SourceList, GetListBoxLines(SourceList, 1));
  208.                     Direction = none;
  209.                     ButtonTitle(MoveButton, "\pMove");
  210.                     EnableButton(MoveButton, off);
  211.                     EnableButton(RemoveButton, off);
  212.                     break;
  213.  
  214.                 case DoneButton:        // We're done, quit the application…
  215.                     ExitTheDemo = true;
  216.                     break;
  217.                 }
  218.                 break;
  219.         }
  220.     }
  221.